package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.mapper.UserMapper;
import qst.com.bacK.pojo.entity.Team;
import qst.com.bacK.pojo.entity.TestPersonnel;
import qst.com.bacK.pojo.entity.User;
import qst.com.bacK.service.ClassTeamService;
import qst.com.bacK.service.TestPersonnelService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/12 下午4:23
 * Description:
 */

@Controller
@RequestMapping("/testPersonnel")
public class TestPersonnelController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TestPersonnelService testPersonnelService;
    @Autowired
    private ClassTeamService classTeamService;

    // 添加
    @RequestMapping("/add.action")
    public void add(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = new User();
        req.getParameter("");
        user.setId(RequestUtil.getInt(req, "id"));
        user.setLogin(RequestUtil.getString(req, "login"));
        user.setPasswd(User.PASSWORD);
        user.setName(RequestUtil.getString(req, "name"));
        user.setType(RequestUtil.getInt(req, "type"));
        user.setStatus(RequestUtil.getInt(req, "status"));
        TestPersonnel s = new TestPersonnel();
        s.setUser(user);
        s.setGender(RequestUtil.getString(req, "gender"));
        s.setPhone(RequestUtil.getString(req, "login"));
        s.setTeamId(RequestUtil.getInt(req, "teamId"));
        s.setBirthDate(RequestUtil.getDate(req, "birthDate"));
        try {
            if (userMapper.findUserByLoginCount(s.getPhone()) != null) {
                req.getSession().setAttribute("_message_key", "该手机号已经存在！");
                int teamId = RequestUtil.getInt(req, "teamId");
                req.setAttribute("teamId", teamId);
                WebUtil.forward(req, resp, "/testPersonnel/create.jsp");
            } else {
                testPersonnelService.addTestPersonnel(s);
                req.getSession().setAttribute("_message_key", "参测人员添加成功");
                resp.sendRedirect("/testPersonnel/select.action?teamId=" + s.getTeamId());
            }
        } catch (ExamException ex) {
            req.setAttribute("user", user);
            req.getSession().setAttribute("_error_key", ex.getMessage());
            WebUtil.forward(req, resp, "/testPersonnel/create.jsp");
        }
    }

    // 删除
    @RequestMapping("/delete.action")
    public void delete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取学生id
        int id = RequestUtil.getInt(req, "id");
        try {
            TestPersonnel testPersonnel = testPersonnelService.deleteTestPersonnel(id);
            resp.sendRedirect("/testPersonnel/list.action?tid=" + testPersonnel.getTeamId());
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
            WebUtil.forward(req, resp, "/testPersonnel/view.action?id=" + id);
        }
    }

    // 编辑
    @RequestMapping("/edit.action")
    public void edit(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取学生id
        int id = RequestUtil.getInt(req, "id");
        TestPersonnel testPersonnel = testPersonnelService.findById(id);
        req.setAttribute("testPersonnel", testPersonnel);
        WebUtil.forward(req, resp, "/testPersonnel/edit.jsp");
    }

    // 导入
    @RequestMapping("/import.action")
    public void importExcel(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int tid = RequestUtil.getInt(req, "tid");
        Part part = req.getPart("file");
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                part.getInputStream()));
        String line;
        List<TestPersonnel> testPersonnelList = new ArrayList<>();
        while ((line = reader.readLine()) != null) {
            String[] data = line.split(",");
            if (data.length < 4) {
                req.getSession().setAttribute("_error_key", "导入文件格式错误:" + line);
                WebUtil.forward(req, resp, "/testPersonnel/import.jsp");
                return;
            }
            TestPersonnel testPersonnel = new TestPersonnel();
            User user = new User();
            testPersonnel.setTeamId(tid);
            testPersonnel.setUser(user);
            testPersonnel.setPhone(data[0]);
            user.setName(data[1]);
            testPersonnel.setGender(data[2]);
            testPersonnel.setBirthDate(Date.valueOf(data[3]));
            testPersonnelList.add(testPersonnel);
        }
        try {
            testPersonnelService.importTestPersonnel(testPersonnelList);
            req.getSession().setAttribute("_message_key", "成功导入" + testPersonnelList.size() + "个参测人员");
            resp.sendRedirect("/testPersonnel/list.action?tid=" + tid);
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            WebUtil.redirect(req, resp, "/team/list.action");
        }
    }

    // 列表
    @RequestMapping("/list.action")
    public void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取班级id
        int tid = RequestUtil.getInt(req, "tid");

        List<TestPersonnel> testPersonnelList = testPersonnelService.findByTeamId(tid);
        req.setAttribute("testPersonnel", testPersonnelList);
        List<Team> teamList = classTeamService.findAll();
        req.setAttribute("teamList", teamList);

        WebUtil.forward(req, resp, "/testPersonnel/list.jsp");
    }

    // 查询
    @RequestMapping("/select.action")
    public void select(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // 获取班级id
        int teamId = RequestUtil.getInt(req, "teamId");
        String name = RequestUtil.getString(req, "stuname");
        String phone = RequestUtil.getString(req, "phone");
        List<TestPersonnel> testPersonnel = testPersonnelService.query(teamId, name, phone);
        req.setAttribute("testPersonnel", testPersonnel);
        List<Team> teamList = classTeamService.findAll();
        req.setAttribute("teamList", teamList);
        WebUtil.forward(req, resp, "/testPersonnel/list.jsp");
    }

    // 更新
    @RequestMapping("/update.action")
    public void update(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        User user = new User();
        user.setId(RequestUtil.getInt(req, "id"));
        user.setName(RequestUtil.getString(req, "name"));
        user.setStatus(Integer.parseInt(RequestUtil.getString(req, "status")));
        TestPersonnel testPersonnel = new TestPersonnel();
        testPersonnel.setId(user.getId());
        testPersonnel.setUser(user);
        Date date = Date.valueOf(RequestUtil.getString(req, "birthDate"));
        testPersonnel.setBirthDate(date);
        testPersonnel.setPhone(RequestUtil.getString(req, "phone"));
        testPersonnel.setGender(RequestUtil.getString(req, "gender"));
        try {
            testPersonnelService.updateTestPersonnel(testPersonnel);
            resp.sendRedirect("/testPersonnel/view.action?id=" + user.getId());
        } catch (ExamException ex) {
            ex.printStackTrace();
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("testPersonnel", testPersonnel);
            WebUtil.forward(req, resp, "/testPersonnel/edit.jsp");
        }
    }

    // view
    @RequestMapping("/view.action")
    public void view(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        TestPersonnel testPersonnel = testPersonnelService.findById(id);
        req.setAttribute("testPersonnel", testPersonnel);
        WebUtil.forward(req, resp, "/testPersonnel/view.jsp");
    }
}